Before Merge
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NUM_SPAWNS 3
main(int argc, char *argv[]) {
int numtasks, rank, dest, source, rc, count;
char inmsg[30], outmsg0[]="Hello Task 2",outmsg1[]="You are Welcome Task 1";
int np = NUM_SPAWNS;
int errcodes[NUM_SPAWNS];
MPI_Comm parentcomm, intercomm;
MPI_Init( &argc, &argv );
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_get_parent( &parentcomm );
if (parentcomm == MPI_COMM_NULL) {
MPI_Comm_spawn( "mpi_spawn1", MPI_ARGV_NULL, np, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm, errcodes );
printf("I'm the parent process pid=%d and rank= %i\n",getpid(),rank);
} else {
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("I'm a spawned process pid=%d and rank= %i\n",getpid(),rank);
}
if (rank == 1) { dest = 2;
source = 2;
MPI_Send(&outmsg0,strlen(outmsg0), MPI_CHAR, dest, 0, MPI_COMM_WORLD);
printf("\nTask 1 has sent its message to Task 2. \n");
MPI_Recv(&inmsg, 30, MPI_CHAR, source, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Task 1 received this message from Task 2 %s\n\n", inmsg);
} else if (rank == 2) { dest = 1;
source = 1;
MPI_Recv(&inmsg,30, MPI_CHAR, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Task 2 received this message: %s\n", inmsg);
printf("Task 2 has not sent its message to Task 1 yet.\n\n");
MPI_Send(&outmsg1,strlen(outmsg1), MPI_CHAR, dest, 1, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
A Run
[siegelj@tc-login MPI_Revisited]$ mpi_spawn1
I'm the parent process pid=17557 and rank= 0
I'm a spawned process pid=17561 and rank= 0
I'm a spawned process pid=17562 and rank= 1
Task 1 has sent its message to Task 2.
Task 1 received this message from Task 2 You are Welcome Task 1
I'm a spawned process pid=17564 and rank= 2
Task 2 received this message: Hello Task 2
Task 2 has not sent its message to Task 1 yet.
[siegelj@tc-login MPI_Revisited]$